package com.symaster.pdfcompose.frame.component.pdfpanel;

import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author yinmiao
 * @version 2024/4/28
 */
public class SourceItem extends JPanel {

    private final OptionBar optionBar;
    private final JComponent component;

    public SourceItem(JComponent component) {
        this.component = component;

        JPanel jPanel = new JPanel(new BorderLayout(5, 0));
        jPanel.setBorder(new EmptyBorder(2, 10, 2, 10));

        optionBar = getOptionBar();
        if (optionBar != null) {
            jPanel.add(this.optionBar, BorderLayout.WEST);
        }

        jPanel.add(component, BorderLayout.CENTER);

        JComponent jToolBar = getRightBar();
        jPanel.add(jToolBar, BorderLayout.EAST);

        setLayout(new BorderLayout());
        setBorder(new LineBorder(new Color(192, 192, 192)));
        add(jPanel, BorderLayout.CENTER);
    }

    JComponent getRightBar() {
        JButton delBtn = getDelBtn();

        JToolBar jToolBar = new JToolBar();
        jToolBar.add(delBtn);
        return jToolBar;
    }

    JButton getDelBtn() {
        JButton delBtn = new JButton(SwingUtil.loadIconEnabledCache("pdf-delete.png", 16, 16));
        delBtn.addActionListener(this::deleteListener);
        return delBtn;
    }

    OptionBar getOptionBar() {
        return new OptionBar(this);
    }

    public JComponent getComponent() {
        return component;
    }

    /**
     * 下移当前行
     */
    void dnListener(ActionEvent e) {
        Container parent = getParent();
        if (parent instanceof PdfPanel) {
            int myIndex;
            if ((myIndex = searchMyIndex(parent)) >= 0 && myIndex < parent.getComponentCount() - 1) {
                parent.add(this, myIndex + 1);
                parent.repaint();
                optionBar.awDnBtn.requestFocus();
            }
        }
    }

    /**
     * 上移当前行
     */
    void upListener(ActionEvent e) {
        Container parent = getParent();
        if (parent instanceof PdfPanel) {
            int i = searchMyIndex(parent);
            if (i > 0) {
                parent.add(this, i - 1);
                parent.repaint();
                optionBar.awUpBtn.requestFocus();
            }
        }
    }

    /**
     * 移动至顶部
     */
    void toTopListener(ActionEvent e) {
        Container parent = getParent();
        if (parent instanceof PdfPanel) {
            int i = searchMyIndex(parent);
            if (i > 0) {
                parent.add(this, 0);
                parent.repaint();
                optionBar.upBtn.requestFocus();
            }
        }
    }

    /**
     * 移动至底部
     */
    void toBottomListener(ActionEvent e) {
        Container parent = getParent();
        if (parent instanceof PdfPanel) {
            int i = searchMyIndex(parent);
            if (i < parent.getComponentCount() - 1) {
                parent.add(this, parent.getComponentCount() - 1);
                parent.repaint();
                optionBar.dnBtn.requestFocus();
            }
        }
    }

    /**
     * 伤处当前行
     */
    void deleteListener(ActionEvent e) {
        Container parent = this.getParent();
        if (parent instanceof PdfPanel) {
            int i = searchMyIndex(parent);
            if (i >= 0) {
                parent.remove(i);
                parent.repaint();
            }
        }
    }

    private int searchMyIndex(Container parent) {
        int myIndex = -1;
        for (int i = 0; i < parent.getComponentCount(); i++) {
            if (parent.getComponent(i) == this) {
                myIndex = i;
            }
        }
        return myIndex;
    }
}
